package com.chinatechstar.component.commons.utils;

import com.chinatechstar.component.commons.constants.HttpConstants;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JDBC工具类
 *
 * @author Rei
 */
public class JDBCUtils {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String dbUrl = null;
    String dbUser = null;
    String dbPassword = null;

    /**
     * 通过构造方法加载数据库驱动
     */
    public JDBCUtils(String driver, String url, String user, String password) {
        try {
            Class.forName(driver);
            dbUrl = url;
            dbUser = user;
            dbPassword = password;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("加载数据库驱动失败！");
        }
    }

    /**
     * 获得数据库连接
     */
    public Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 设置参数
     */
    public PreparedStatement setPs(PreparedStatement ps, Object[] obj) {
        if (obj != null) {
            for (int i = 0; i < obj.length; i++) {
                try {
                    ps.setObject(i + 1, obj[i]);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return ps;
    }

    /**
     * 通用的增删改
     */
    public boolean executeUpdate(String sql, Object[] obj) {
        conn = getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps = setPs(ps, obj);
            int i = ps.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(ps, null, conn);
        }
        return false;
    }

    /**
     * 通用的查询方法
     */
    public List<Map<String, Object>> executeQuery(String sql, Object[] obj) {
        conn = getConnection();
        List<Map<String, Object>> list = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps = setPs(ps, obj);
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                for (int i = 0; i < rsmd.getColumnCount(); i++) {
                    map.put(rsmd.getColumnName(i + 1).toLowerCase(), rs.getObject(i + 1));
                }
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(ps, rs, conn);
        }
        return list;
    }

    /**
     * 关闭连接
     */
    public void closeAll(PreparedStatement ps, ResultSet rs, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
